$(function() {
	$.t = {
		UIDelete: "删除",
		UISubmit: "确定",
		UISearch: "搜索",
		UISearchLoading: "搜索中..",
		UIComplete: "完成",
		UIClear: "清除",
		UICancel: "取消",
		UIDay: "日",
		UIWeek: "周",
		UIYear: "年",
		UIMonth: "月",
		UIHour: "时",
		UIMinute: "分",
		UINumber: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
	};
	$.UIPopupTimer = 700;
	$.UIPopup = function(options, callback) {
		var self = this;
		var settings = {
			"html": "",
			"timer": $.UIPopupTimer
		}
		if (typeof(options) == "string" || $.isArray(options)) {
			settings.html = options
		} else {
			$.extend(settings, options);
		};
		$("body").append("<div m-role=\"UIPopup\">" + settings.html + "</div>");
		var p = $("[m-role='UIPopup']");
		var l = -p.outerWidth() / 2;
		var t = -p.outerHeight() / 2;
		p.css({
			"margin-left": l,
			"margin-top": t,
			"top": "50%",
			"left": "50%"
		})
		
		if (settings.html.length >10) {
			settings.timer = "2400";
		} else if (settings.html.length >8) {
			settings.timer = "2000"
		} else if (settings.html.length >6) {
			settings.timer = "1500"
		} else {
			settings.timer = "1200"
		}
		p.animate({
			"opacity": 1
		}, 200).delay(settings.timer).animate({
			"opacity": 0
		}, 200, function() {
			$(this).remove();
			callback && callback.call(self);
		})
	};
	$.UIConfirm = function(options, callback) {
		$.UIComfirm(options, callback)
	};
	$.UIComfirm = function(options, callback) {
		var self = this;
		if ($("[m-role='UIComfirm']").size() > 0)
			return;
		var settings = {
			"html": "",
			"submitText": $.t.UISubmit,
			"submitStyle": "confirm",
			"cancelText": $.t.UICancel,
			"cancelStyle": "confirm"
		}
		if (typeof(options) == "string") {
			settings.html = options
		} else {
			$.extend(settings, options);
		};
		var layout = ["<div m-role=\"UIComfirm\">", "<div class=\"UIComfirmContent\">", "<div m-role=\"cell\" m-mode=\"y\">", "<div m-role=\"cell\" class=\"UIComfirmContentHtml\" m-align=\"center\">", settings.html, "</div>", "<div m-role=\"cell\" m-mode=\"x-equal\" class=\"UIConfirmBtns\">", "<div m-role=\"cell\" m-layout=\"fit\">", "<div m-role=\"UIButton\" m-style=\"" + settings.cancelStyle + "\" m-size=\"min\" m-layout=\"fit\" class=\"UIComfirmCancel\">" + settings.cancelText + "</div>", "</div>", "<div m-role=\"cell\" m-layout=\"fit\">", "<div m-role=\"UIButton\" m-style=\"" + settings.submitStyle + "\" m-size=\"min\" m-layout=\"fit\" class=\"UIComfirmSubmit\">" + settings.submitText + "</div>", "</div>", "</div>", "</div>", "</div>", "</div>"].join("")
		$("body").append(layout);
		var c = $("[m-role='UIComfirm']")
		var p = $(".UIComfirmContent");
		var l = -p.outerWidth() / 2;
		var t = -p.outerHeight() / 2;
		p.css({
			"margin-left": l,
			"margin-top": t,
			"top": "50%",
			"left": "50%"
		});
		setTimeout(function() {
			p.bind("scrollstart", function() {
				return false
			})
		}, 300);
		$(".UIComfirmSubmit").bind("click", function() {
			callback && callback.call(self, "yes");
			c_hide();
			return false;
		});
		$(".UIComfirmCancel").bind("click", function() {
			callback && callback.call(self, "no");
			c_hide();
			return false;
		});

		function c_hide() {
			c.hide();
			setTimeout(function() {
				c.remove()
			}, 200)
		}
	};
	$.UIAlert = function(options, callback) {
		var self = this;
		if ($("[m-role='UIAlert']").size() > 0)
			return;
		var settings = {
			"html": "",
			"buttonText": $.t.UISubmit,
			"buttonStyle": "alert"
		}
		if (typeof(options) == "string") {
			settings.html = options
		} else {
			$.extend(settings, options);
		};
		var layout = ["<div m-role=\"UIAlert\">", "<div class=\"UIAlertContent\">", "<div m-role=\"cell\" m-mode=\"y\">", "<div m-role=\"cell\" class=\"UIAlertContentHtml\">", settings.html, "</div>", "<div m-role=\"cell\">", "<div m-role=\"UIButton\" m-style=\"" + settings.buttonStyle + "\" m-size=\"min\" m-layout=\"fit\" class=\"UIAlertSubmit\">" + settings.buttonText + "</div>", "</div>", "</div>", "</div>", "</div>"].join("")
		$("body").append(layout);
		var c = $("[m-role='UIAlert']")
		var p = $(".UIAlertContent");
		var l = -p.outerWidth() / 2;
		var t = -p.outerHeight() / 2;
		p.css({
			"margin-left": l,
			"margin-top": t,
			"top": "50%",
			"left": "50%"
		});
		setTimeout(function() {
			p.bind("scrollstart", function() {
				return false
			})
		}, 300);
		$(".UIAlertSubmit").bind("click", function() {
			callback && callback.call(self, "yes");
			c_hide();
			return false;
		});

		function c_hide() {
			c.hide();
			setTimeout(function() {
				c.remove()
			}, 200)
		}
	};
	$.UILoadingIos = function() {
		var d,
		a = arguments,
		b = {
			self : "[m-role='UILoading']"
		},
		c = {
			_init : function () {
				c._events()
			},
			_events : function () {
				setTimeout(function () {
					$(b.self).bind("scrollstart", function () {
						return !1
					})
				}, 300)
			},
			_html : function () {
				return ['<div m-role="UILoading">', '<div class="UILoadingContent">', '<div class="UILoadingContentText"></div>', "</div>", "</div>"].join("")
			},
			_fn : {
				_html : function () {
					0 == $(b.self).size() && $("body").append(c._html())
				},
			}
		};
		c._init(),
		d = {
			show : function () {
				var a = arguments[0];
				c._fn._html(),
				$(".UILoadingContent").css("opacity", "0"),
				$(b.self).find(".UILoadingContentText").html(a[1] || ""),
				$(b.self).removeClass("UILoadingMin"),
				$(b.self).show(),
				$(".UILoadingContent").animate({
					opacity : 1
				}, 200)
			},
			showMin : function () {
				var a = arguments[0];
				c._fn._html(),
				$(b.self).addClass("UILoadingMin"),
				$(b.self).find(".UILoadingContentText").html(a[1] || ""),
				$(b.self).show()
			},
			hide : function () {
				var a = arguments[0],
				c = this;
				$(b.self).stop().animate({
					opacity : 0
				}, 500, function () {
					$(b.self).hide(),
					setTimeout(function () {
						$(b.self).remove()
					}, 500),
					a[1] && a[1].call(c)
				})
			}
		},
		d[a[0]](a);
	};
})